#include "../inc/Chess/general.h"
#include <iostream>
using namespace std;

General::General(PieceType type, int row, int col, bool isRed)
    : Piece(type, row, col, isRed)
{
}

bool General::is_valid_move(int to_row, int to_col, PieceType board[9][10])
{
    int row = position.first;
    int col = position.second;

    // 计算移动距离
    int row_diff = to_row - row;
    int col_diff = to_col - col;

    if (abs(row_diff) + abs(col_diff) != 1)
    {
        if (row != to_row)
        {
            return false;
        }

        // 确定对方将/帅的类型
        PieceType enemy_general = isRed ? PieceType::Bjiang : PieceType::Rshuai;

        // 检查目标位置是否是对方将/帅
        if (board[to_row][to_col] != enemy_general)
        {
            return false;
        }

        // 检查中间是否有棋子阻隔
        int min_col = min(col, to_col);
        int max_col = max(col, to_col);
        for (int r = min_col + 1; r < max_col; ++r)
        {
            if (board[row][r] != PieceType::kong)
            {
                return false;
            }
        }
        return true;
    }

    if (isRed)
    {
        if (to_col <= 2 && (to_row <= 5 && to_row >= 3))
        {
            return true;
        }
    }
    else
    {
        if (to_col >= 7 && (to_row <= 5 && to_row >= 3))
        {
            return true;
        }
    }

    return false;
}